Skip to content

Conversation

gkoreman
Copy link
Contributor

@gkoreman gkoreman commented Jun 26, 2024

Fixes #2477

Include all data from cc_libraries as runfiles or resources, depending on aspect_hints.
Provide aspect_hints for resource collection for all resource targets (eg, swift_library, cc_library, etc)

By default all data is now included from cc_libraries as runfiles and follows the expected runfiles folder structure, with files retaining their nested folders and being placed in /external/pkg_name/some/file.txt when included from a http_archive or local_repository.

The default behavior for other resource collecting targets like swift_library has not changed, and will continue to collect and process data as resources.

To allow modification of this default behavior, users may add aspect_hints to the target cc_library/swift_library/etc. There are three supported aspect_hints:
@build_bazel_rules_apple//apple:use_runfiles
@build_bazel_rules_apple//apple:use_resources
@build_bazel_rules_apple//apple:suppress_resources

Example

Here is an example of modifying the default behavior to bundle data.txt as a resource instead of a runfile.

cc_library(
    name = "libapp",
    srcs = ["main.cpp",],
    data = [":data.txt"],
    aspect_hints = ["@build_bazel_rules_apple//apple:use_runfiles"],
)

macos_application(
    name = "app_macos",
    deps = [":libapp"],
)

data.txt is bundled in Contents/Resources/data.txt

Note

Hints apply only to the target and do not affect transitive deps, however if a target includes runfiles then all runfiles are bundled (including transitive runfiles) regardless of the hints applied to transitive targets.

@brentleyjones
Copy link
Collaborator

brentleyjones commented Jun 26, 2024

I would like to flip the default, such that runfiles are collected by default for cc_library (but only it and the rules that currently don’t collect resources), and you need to use the aspect hint to make new rules collect resources instead. This will make it so the least number of people targets to use aspect hints. We will then need another aspect hint that tells the currently support resource collection rules to use runfiles instead. And we can have it be a build error when both are provided.

gkoreman added 7 commits June 26, 2024 10:31
Three aspect hints are pre-defined and can be used on any target:
@build_bazel_rules_apple//apple:use_resources
@build_bazel_rules_apple//apple:use_runfiles
@build_bazel_rules_apple//apple:suppress_resources
Runfiles may be placed in the external/repo_name_x/ folder if they are pulled in from http_archive or local_repository. Runfiles from the main repo will be in a folder structure matching the main repo.
@gkoreman gkoreman marked this pull request as ready for review July 5, 2024 06:04
Copy link
Contributor

@luispadron luispadron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Left some comments I think we should address.

You'll also need to re-run buildifier as we updated the version and it adds more lints/formatting fixes

@gkoreman gkoreman requested a review from luispadron July 25, 2024 18:36
@luispadron
Copy link
Contributor

@brentleyjones Would you have some time to review this as well? Thank you!

@gkoreman
Copy link
Contributor Author

gkoreman commented Aug 6, 2024 via email

@brentleyjones brentleyjones merged commit 91be02a into bazelbuild:master Aug 7, 2024
@gkoreman gkoreman deleted the use_runfiles branch August 8, 2024 00:15
@jiawen
Copy link
Contributor

jiawen commented Dec 14, 2024

Hints apply only to the target and do not affect transitive deps, however if a target includes runfiles then all runfiles are bundled (including transitive runfiles) regardless of the hints applied to transitive targets.

I had a hard time parsing the second part of this sentence. After some experimentation, I discovered that if use_resources is set for a leaf cc_library, then for it to be propagated correctly, intermediate cc_library targets also need to have use_resources set. That is, all targets up the tree from a leaf cc_library up to a resource collecting target (an objc_library (and probably swift_library I imagine) need to have use_resources set for it to be processed and bundled correctly.

@gkoreman I'm sure you meant this to be the case in your note, but my brain had a hard time parsing this. Of course it makes sense in hindsight.

I didn't have a chance to experiment with the other three common cases:

  1. Every target up the tree sets use_runfiles instead. I expect the behavior to be the same.
  2. The direct dep has use_resources but transitive deps set use_runfiles.
  3. The opposite: direct dep has use_runfiles but transitive deps set use_resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Runfiles not bundled with macos_application
4 participants